package com.zyfdroid.bigdialerkit;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.zyfdroid.bigdialerkit.dao.MissingCall;
import com.zyfdroid.bigdialerkit.receivers.CallingChangedReceiver;
import com.zyfdroid.bigdialerkit.services.MissedCallsRemoteViewsFactory;
import com.zyfdroid.bigdialerkit.utils.BaseActivity;

public class MissedCallDetailActivity extends BaseActivity {
    MissingCall missedCall;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_missed_call_detail);
        // 从Intent中提取数据
        Intent intent = getIntent();
        int _id = intent.getIntExtra("_id", -1);
        String displayName = intent.getStringExtra("displayName");
        String phoneNumber = intent.getStringExtra("phoneNumber");
        long callTime = intent.getLongExtra("callTime", -1);

        // 创建MissingCall对象
        missedCall = new MissingCall(_id, displayName, phoneNumber, callTime);

        // 使用missedCall对象进行后续操作
        if(_id == -1){
            finish();
        }

        ((TextView)findViewById(R.id.txtMissedCallName)).setText(missedCall.displayName);
        ((TextView)findViewById(R.id.txtMissedCallDetail)).setText(MissedCallsRemoteViewsFactory.FormatDateDiff(System.currentTimeMillis(),missedCall.callTime)+"打过电话");

    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }

    private void deleteCallLog(int _id){
        // 定义通话记录的Uri
        Uri CALLLOG_URI = Uri.parse("content://call_log/calls");

        // 使用ContentResolver删除指定_id的通话记录
        ContentResolver contentResolver = getContentResolver();
        int deletedRows = contentResolver.delete(
                CALLLOG_URI,
                "_id = ?",
                new String[]{String.valueOf(_id)}
        );

        // 检查是否成功删除
        if (deletedRows > 0) {
            // 删除成功，发送广播通知
            showToast("已删除未接来电");

        }
        else{
            showToast("未能删除未接来电");

        }
    }

    public void missedCallDismiss(View view) {
        CallingChangedReceiver.notifyUpdate(this);
        deleteCallLog(missedCall._id);
        finish();
    }

    public void missedCallCallback(View view) {
        deleteCallLog(missedCall._id);
        CallingChangedReceiver.notifyUpdate(this);
        callPhone(missedCall.phoneNumber);
        finish();
    }

    public void missedCallClose(View view) {
        finish();
    }
}
